/**
 * 
 */
package org.vsg.stock.basic.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.vsg.stock.basic.Stock;

/**
 * @author vison
 *
 */
public class StockServiceImpl implements StockService , InitializingBean{

	private final static Logger logger = LoggerFactory.getLogger(StockServiceImpl.class);	
	
	/**
	 * set the read config
	 */
	private File mapPath;

	/**
	 * parse the property file 
	 */
	private String encoding;

	private List<Stock> allStocks = new ArrayList<Stock>();	
	
	private List<Stock> availStocks;
	
	private List<String> allStockCode = new ArrayList<String>();	
	

	
	public File getMapPath() {
		return mapPath;
	}

	public void setMapPath(File mapPath) {
		this.mapPath = mapPath;
	}

	@Override
	public List<Stock> getAllStocks() {
		// TODO Auto-generated method stub
		return allStocks;
	}
	
	@Override
	public List<Stock> getAvaiableStocks() {
		// TODO Auto-generated method stub
		return availStocks;
	}



	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	/**
	 * set the init call back
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

		readData();
		
		availStocks = filterAvaiableStocks();
		
	}	
	
	private List<Stock> filterAvaiableStocks() throws Exception {
		List<Stock> avail = new ArrayList<Stock>();
		
		Stock stock = null;
		for(Iterator<Stock> iterAll = allStocks.iterator() ; iterAll.hasNext() ; ) {
			stock = iterAll.next();
			
			if (stock.getType().equalsIgnoreCase("SH")) {
				if (stock.getCode().startsWith("00") 
						|| stock.getCode().startsWith("60")
						|| stock.getCode().startsWith("90")
					) {
					
					if (stock.getCode().startsWith("00")) {
						stock.setKind("G");
					} else {
						stock.setKind("R");
					}
				
					allStockCode.add( stock.getType() + stock.getCode() );
					avail.add( stock );
					
				}
			} else if ( stock.getType().equalsIgnoreCase("SZ") ) {
				if (stock.getCode().startsWith("399") 
						|| stock.getCode().startsWith("00") 
						|| stock.getCode().startsWith("30") 
					) {
					if (stock.getCode().startsWith("399")) {
						stock.setKind("G");
					} else {
						stock.setKind("R");
					}

					allStockCode.add( stock.getType() + stock.getCode() );					
					avail.add( stock );
					
				}				
			}
			
		}
		
		
		return avail;
	}

	
	
	@Override
	public List<String> getAvaiableStockCodes() {
		// TODO Auto-generated method stub
		return allStockCode;
	}
	
	

	private void readData() throws Exception {
		// --- get the current OS encoding ---
		// list the children filed
		File[] files = mapPath.listFiles();

		for (File file : files) {
			String typeCode = file.getName().substring(0,2);
			// --- read the list parse to object ---
			if (!file.isDirectory()) {
				LineIterator it = FileUtils.lineIterator(file, encoding);
				try {
					while (it.hasNext()) {
						String line = it.nextLine().trim();
						// / do something with line
						String[] map = line.split("\t");
						if (map.length == 2) {
							Stock stock = new Stock();
							stock.setType( typeCode );
							stock.setCode( map[0] );
							stock.setName( map[1] );
							allStocks.add( stock );
							logger.debug( stock.toString() );
						}

					}
				} finally {
					LineIterator.closeQuietly(it);
				}
				
			}

		}
	}	
	
}
